package com.syl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syl.dao.ArticleMapper;
import com.syl.dao.CommentMapper;
import com.syl.dao.StatisticMapper;
import com.syl.model.domain.Article;
import com.syl.model.domain.Statistic;
import com.syl.service.IArticleService;
import com.vdurmont.emoji.EmojiParser;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ArticleServiceImpl implements IArticleService {

  @Autowired
  private ArticleMapper articleMapper;
  @Autowired
  private StatisticMapper statisticMapper;
  @Autowired
  private RedisTemplate redisTemplate;
  @Autowired
  private CommentMapper commentMapper;

  // 分页查询博文列表
  @Override
  public PageInfo<Article> selectArticleWithPage(Integer page, Integer count) {
    // 设置分页信息, 相当于 limit 后面的参数
    PageHelper.startPage(page, count);
    List<Article> articleList = articleMapper.selectArticleWithPage();
    // 封装博文统计数据
    for (int i = 0; i < articleList.size(); i++) {
      Article article = articleList.get(i);
      Statistic statistic = statisticMapper.selectStatisticWithArticleId(article.getId());
      article.setHits(statistic.getHits());
      article.setCommentsNum(statistic.getCommentsNum());
    }
    return new PageInfo<>(articleList);
  }

  // 统计前10的热度博文信息
  @Override
  public List<Article> getHeatArticles() {
    List<Statistic> list = statisticMapper.getStatistic();
    List<Article> articlelist = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
      Article article = articleMapper.selectArticleWithId(list.get(i).getArticleId());
      article.setHits(list.get(i).getHits());
      article.setCommentsNum(list.get(i).getCommentsNum());
      articlelist.add(article);
      // 如果当前列表中博文数量已经有 10 个了, 就跳出循环
      if (i >= 9) {
        break;
      }
    }
    return articlelist;
  }

  // 根据id查询单个博文详情，并使用Redis进行缓存管理
  public Article selectArticleWithId(Integer id) {
    Article article = null;
    Object o = redisTemplate.opsForValue().get("article_" + id);// Redis 拿取缓存
    if (o != null) {
      article = (Article) o;
    } else {
      article = articleMapper.selectArticleWithId(id);
      if (article != null) {
        redisTemplate.opsForValue().set("article_" + id, article);// 加入缓存
      }
    }
    return article;
  }

  // 发布博文
  @Override
  public void publish(Article article) {
    // 去除表情
    article.setContent(EmojiParser.parseToAliases(article.getContent()));
    article.setCreated(new Date());
    article.setHits(0);
    article.setCommentsNum(0);
    // 插入博文，同时插入博文统计数据
    articleMapper.publishArticle(article);
    statisticMapper.addStatistic(article);
  }

  // 更新博文
  @Override
  public void updateArticleWithId(Article article) {
    article.setModified(new Date());
    articleMapper.updateArticleWithId(article);
    redisTemplate.delete("article_" + article.getId());
  }

  // 删除博文
  @Override
  public void deleteArticleWithId(int id) {
    // 删除博文的同时，删除对应的缓存
    articleMapper.deleteArticleWithId(id);
    redisTemplate.delete("article_" + id);
    // 同时删除对应博文的统计数据
    statisticMapper.deleteStatisticWithId(id);
    // 同时删除对应博文的评论数据
    commentMapper.deleteCommentWithId(id);
  }

}

